package two.chapter_1.c1_4;

import edu.princeton.cs.algs4.StdOut;
import two.chapter_1.c1_3.Collection;
import two.chapter_1.c1_3.LinkStack;
import two.chapter_1.c1_3.Queue;
import two.chapter_1.c1_3.Stack;

import java.util.Iterator;

public class Ex1_4_27 {


    public static void main(String[] args) {

        StackImplQueue<String> queue=new StackImplQueue<>();

        queue.enqueue("a");
        queue.enqueue("b");
        queue.enqueue("c");
        queue.enqueue("d");
        queue.enqueue("e");
        queue.enqueue("f");
        queue.enqueue("g");

        while (!queue.isEmpty()){
            StdOut.print(queue.dequeue());
        }
    }



    private static class StackImplQueue<Item> implements Queue<Item> {
        private Stack<Item> stack_1;
        private Stack<Item> stack_2;
        private int size;

        public StackImplQueue(){
            stack_1=new LinkStack<>();
            stack_2=new LinkStack<>();
        }
        @Override
        public boolean isEmpty() {
            return size()==0;
        }

        @Override
        public int size() {
            return size;
        }

        @Override
        public Iterator<Item> iterator() {
            return null;
        }

        @Override
        public void enqueue(Item item) {
            stack_1.push(item);
            size++;
        }

        @Override
        public Item dequeue() {
            if (stack_2.isEmpty()) {
                while (!stack_1.isEmpty()) {
                    stack_2.push(stack_1.pop());
                }
            }
            size--;
            return stack_2.pop();
        }
    }

}
